{include file='/public/header'}

<div class="am-cf admin-main">
    <!-- content start -->
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">添加文章</strong> / <small>add article</small></div>
            </div>

            <hr/>

            <div class="am-g">
                <div class="am-u-lg-12">
                    <form class="am-form" action="{:url('/admin/article/edit')}" method="post">
                        <input type="hidden" name="id" value="{$info.id}" />
                        <input type="hidden" name="doc_id" value="{$info.doc_id}" />
                        <div class="am-form-group am-form-inline am-g">
                            <div class="am-u-lg-2">
                                <input type="text" id="title" name="title" value="{$info.title}" placeholder="请输入文章标题">
                            </div>
                            {if(!$info['doc_id'])}
                            <div class="am-u-lg-3">
                                <label class="am-form-label">所属分类</label>
                                <select id="top_categories" data-am-selected="{btnWidth:'38%',btnSize:'md'}">
                                    {foreach $top_categories as $category}
                                    <option value="{$category.id}" {if($selected_top_category['id'] == $category['id'])}selected="selected"{/if}>{$category.name}</option>
                                    {/foreach}
                                </select>
                                <select name="category_id" data-am-selected="{btnWidth:'38%',btnSize:'md'}" id="sec_categories">
                                    {foreach $sec_categories as $category}
                                    <option value="{$category.id}" {if($selected_sec_category['id'] == $category['id'])}selected="selected"{/if}>{$category.name}</option>
                                    {/foreach}
                                </select>
                            </div>
                            {/if}
                            {if ( $info['doc_id'] > 0 )}
                            <div class="am-u-lg-3">
                                <label class="am-form-label">所属目录</label>
                                <select id="top_catelog" name="top_catelog_id" data-am-selected="{btnWidth:'38%',btnSize:'md'}">
                                    {foreach $top_catelog as $catelog}
                                    <option value="{$catelog.id}" {if($selected_top_catelog['id'] == $catelog['id'])}selected="selected"{/if}>{$catelog.name}</option>
                                    {/foreach}
                                </select>
                                <select name="doc_catelog_id" data-am-selected="{btnWidth:'38%',btnSize:'md'}" id="sec_catelog">

                                </select>
                            </div>
                            {/if}
                            <div class="am-u-lg-2">
                                <input type="text" id="sort" name="sort" value="{$info.sort}" placeholder="可选：顺序数字">
                            </div>
                            <div class="am-u-lg-2">
                                <button type="submit" class="am-btn am-btn-primary">保存</button>
                                <button type="button" class="am-btn am-btn-danger" onclick="javascript:history.back(-1);">取消</button>
                                <label class="am-checkbox-inline"><input type="checkbox" id="is_publish" name="is_publish" value="1" {if($info['is_publish'])}checked="checked"{/if}>发布</label>
                            </div>
                        </div>
                        <div id="editor">
                            <textarea style="display: none;">{$info.content}</textarea>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- content end -->
</div>
{include file='/public/footer'}
<script src="/static/common/editor.md/editormd.js"></script>
<link rel="stylesheet" href="/static/common/editor.md/css/editormd.css" />
<script>
    $(document).ready(function(){

        window.phpby.main.editor('editor');

        $('#top_categories').on('change',function(e){
            $('#sec_categories').empty();
            var top_category_id = $(this).val();
            var url = "{:url('admin/category/ajax_get_sec_categories')}";
            var data = {top_category_id:top_category_id};
            $.ajax({
                url: url,
                data:data,
                success:function(res){
                    if( res.code == 1 ){
                        var resultData = res.data;
                        for(key in resultData){
                            $('#sec_categories').append('<option value="'+resultData[key]['id']+'">'+resultData[key]['name']+'</option>');
                        }
                    }
                },
                error:function(res){
                    layer.msg('数据返回错误',{icon:2});
                }
            }).fail(function(){
                layer.msg('网络错误',{icon:2});
            });
        });

        $('#top_catelog').on('change',function(e){
            $('#sec_catelog').empty();
            $('#sec_catelog').append('<option value="0">选择二级目录</option>');
            var top_catelog_id = $(this).val();
            if( top_catelog_id > 0 ){
                var url = "{:url('admin/doccatelog/ajax_get_sec_catelog')}";
                var data = {top_catelog_id:top_catelog_id};
                $.ajax({
                    url: url,
                    data:data,
                    success:function(res){
                        if( res.code == 1 ){
                            var resultData = res.data;
                            for(key in resultData){
                                $('#sec_catelog').append('<option value="'+resultData[key]['id']+'">'+resultData[key]['name']+'</option>');
                            }
                        }
                    },
                    error:function(res){
                        layer.msg('数据返回错误',{icon:2});
                    }
                }).fail(function(){
                    layer.msg('网络错误',{icon:2});
                });
            }
        });
    });
</script>